// src/composables/usePileData.js
import { ref } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import axios from "axios";

export function usePileData() {
    // 列表相关
    const piles = ref([]);
    const selectedPiles = ref([]);
    const page = ref(1);
    const size = ref(5);
    const total = ref(0);
    const loading = ref(false);

    // 筛选条件
    const pileName = ref("");
    const pileCode = ref("");
    const pileStatus = ref(null);

    // 1) 获取列表
    const fetchData = () => {
        if (loading.value) return;
        loading.value = true;

        const params = { page: page.value, size: size.value };
        if (pileName.value) params.pileName = pileName.value.trim();
        if (pileCode.value) params.pileCode = pileCode.value.trim();
        if (pileStatus.value && pileStatus.value !== "全部")
            params.pileStatus = pileStatus.value.trim();

        axios.get("/chargingPile/getChargingPileByPage", { params })
            .then((response) => {
                const result = response.data;
                piles.value = result.records || [];
                total.value = result.total || 0;

                // 动态计算最大页数
                const maxPage = Math.ceil(total.value / size.value) || 1;
                if (page.value > maxPage) {
                    page.value = maxPage;
                    fetchData(); // 重新获取
                }
            })
            .catch(() => {
                ElMessage.error("数据加载失败");
                piles.value = [];
            })
            .finally(() => {
                loading.value = false;
            });
    };

    // 2) 多选
    const handleSelectionChange = (selection) => {
        selectedPiles.value = selection;
    };

    // 3) 删除
    const deletePile = (row, callback) => {
        ElMessageBox.confirm("确认删除该充电桩吗？", "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
        })
            .then(() => {
                axios
                    .delete("/chargingPile/deleteChargingPile", { params: { id: row.id } })
                    .then(() => {
                        ElMessage.success("删除成功");
                        callback && callback();
                    })
                    .catch(() => {
                        ElMessage.error("删除失败");
                    });
            })
            .catch(() => {
                ElMessage.info("已取消操作");
            });
    };

    // 4) 批量删除
    const batchDelete = (callback) => {
        if (selectedPiles.value.length === 0) {
            ElMessage.warning("请先选择数据");
            return;
        }
        const ids = selectedPiles.value.map((p) => p.id);
        axios
            .post("/chargingPile/batchDelete", ids)
            .then(() => {
                ElMessage.success("批量删除成功");
                callback && callback();
            })
            .catch(() => {
                ElMessage.error("批量删除失败");
            });
    };

    return {
        piles,
        selectedPiles,
        page,
        size,
        total,
        loading,
        pileName,
        pileCode,
        pileStatus,

        fetchData,
        handleSelectionChange,
        deletePile,
        batchDelete,
    };
}
